# We want to set the working directory to our folder automatically.rm(list =ls(all =TRUE)) # Clear environment#check working directory (should be project folder)getwd()# Load packageslist_of_packages <-c("quarto", # to run Quarto file"knitr", # to run Quarto file"rmarkdown", # to run Quarto file"maps", # for maps"countrycode", # for maps"tidyverse","dplyr","readxl", "mapproj", # For Gall-Peters projection"paletteer", # to create continuous color scale"ggthemes"# also needed for color scale in combination with paletteer)new_packages <- list_of_packages[!(list_of_packages %in%installed.packages()[, "Package"])]if (length(new_packages))# Install any new packagesinstall.packages(new_packages)# Library in all packageslapply(list_of_packages, require, character.only =TRUE)# Clean workspacerm(list_of_packages, new_packages)require(viridis)theme_set(theme_void() )# import datadata <-read_excel("data.xlsx", sheet ="Data")View(data)# filter only included paper excluded <-filter(data, Include==0)View(excluded)nrow(excluded)table(excluded$"Reason to exclude")# filter only included paper clean_data <-filter(data, Include ==1)View(clean_data)# Sanity check. Should be 230.nrow(clean_data)rm(data, excluded)# create world map coordinatesworld_coordinates <-map_data("world")# create dataframemap_df <-data.frame(clean_data$`Country (of participants)`) map_df <- map_df %>%rename(countries ="clean_data..Country..of.participants..")View(map_df)ls(map_df)map_df_clean <- map_df %>%mutate(countries =case_when(str_detect(countries, "Congo, Republic of the") ~"Republic of Congo",str_detect(countries, "Korea South") ~"South Korea",is.na(countries) | countries ==""~"Does not say!", # one paper which doesn't specify where participants were fromTRUE~ countries ))View(map_df_clean)# Step 1: Split multi-country strings and unnest into rowscountry_mentions <- map_df_clean %>%separate_rows(countries, sep =",\\s*") %>%filter(countries !="") %>%mutate(countries =str_trim(countries)) # remove any extra whitespace# Sanity checkView(country_mentions)nrow(country_mentions)# Step 2: Count how many times each country is mentionedcountry_counts <- country_mentions %>%count(countries, sort =TRUE) %>%rename(country = countries, frequency = n)# Step 3: Prepare map dataworld_map <-map_data("world") %>%mutate(country = region)# Standardize country names to improve joinscountry_counts$country_standard <-countrycode(country_counts$country, "country.name", "country.name")View(country_counts)
List of frequencies
Show the code
# create table with country name and frequencytwocolumns <-select(country_counts,1:2)knitr::kable(twocolumns)
country
frequency
United States
83
United Kingdom
28
Germany
26
Australia
23
Canada
17
China
12
Italy
12
Norway
11
Finland
10
India
9
Sweden
9
Spain
8
Netherlands
7
Austria
6
Brazil
6
Global (>50)
6
Ireland {Republic}
6
New Zealand
6
Portugal
6
Belgium
5
Denmark
5
Israel
5
Lithuania
5
Russian Federation
5
Switzerland
5
Turkey
5
Bangladesh
4
France
4
Slovenia
4
Taiwan
4
Croatia
3
Czech Republic
3
Hungary
3
Japan
3
Poland
3
Singapore
3
Bulgaria
2
Chile
2
Cyprus
2
Estonia
2
Iceland
2
Indonesia
2
Latvia
2
Mexico
2
Nigeria
2
Peru
2
Slovakia
2
South Africa
2
South Korea
2
Tanzania
2
Uganda
2
Azerbaijan
1
Belarus
1
Belize
1
Does not say!
1
Ethiopia
1
Europe
1
Georgia
1
Greece
1
Kenya
1
Luxembourg
1
Malaysia
1
Malta
1
Oman
1
Pakistan
1
Philippines
1
Republic of Congo
1
Romania
1
Somalia
1
Tonga
1
United Arab Emirates
1
Uruguay
1
Venezuela
1
Merge counts with map
Show the code
# create mapworld_map$country_standard <-countrycode(world_map$country, "country.name", "country.name")View(world_map)# Step 4: Join counts to the mapmap_with_counts <- world_map %>%left_join(country_counts, by ="country_standard", relationship ="many-to-many")View(map_with_counts)
create map
Show the code
blue_palette <- paletteer::paletteer_c("ggthemes::Blue", 17)# Step 5: Plot the mapclearer <- map_with_counts %>%mutate(text =paste("Country: ", country_standard, "\nStudies (n): ", frequency, sep="")) %>%ggplot(aes(x = long, y = lat, group = group, fill =factor(frequency), text = text)) +geom_polygon(linewidth=0.005, alpha=0.9) +theme_void() +scale_fill_manual(values = blue_palette,na.value ="#f0f0f0", name="Number of studies",breaks =seq(85),guide =guide_legend(keyheight =unit(3, units ="mm"),keywidth=unit(10, units ="mm"), label.position ="bottom", title.position ='top', nrow=1)) +labs(title ="") +theme(legend.position =c(0.51, 0.09),plot.title =element_text(hjust =0.5)) +coord_fixed(1.3); clearer
Interactive map
Show the code
plotly::ggplotly(clearer)
Show the code
ggsave("map.jpeg", dpi =700, width =10, height =6, units ="in")